package com.jqb.struts2lab01.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	@SuppressWarnings("rawtypes")
	public Object convertFromString(Map context, String[] values, Class toClass) {
		Date date = null;
		try {
			date = sdf.parse(values[0]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("fromstring:"+date);
		return date;
	}

	@Override
	@SuppressWarnings("rawtypes")
	public String convertToString(Map context, Object o) {
		Date d = (Date)o;
		String date =sdf.format(d);
		System.out.println("toString:"+date);
		return date;
	}

}
